﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DomainModel;
using Utils;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;

namespace WebUI.Models
{
    public class MessageShowViewModel: BaseViewModel
    {
        public MessageShowViewModel()
            : base()
        { }

        public string Message { get; set; }

        public IndLogin Receiver { get; set; }
        public EmailLog EmailLog { get; set; }

        public MessageShowViewModel(string receiptID)
            : base(new { ReceiptID = receiptID})
        {

        }

        protected override void SetVarsWhichDependOnDynamicParams(dynamic dynParams)
        {
            //this id is userName
            string receiptID = dynParams.ReceiptID;
            Receiver = DataUtils.GetIndLoginByUserName(receiptID);

            EmailLog = DataUtils.GetOne<EmailLog>(ConstUtils.SP_GetOneEmailLogByUserName, new { SenderLoginID = Identity.LoginID, ReceiverUserName = receiptID });
            if (EmailLog != null && !string.IsNullOrEmpty(EmailLog.Body))
            { 
                string key1 = "------<br />";
                string key2 = "<br />------";
                int index1 = EmailLog.Body.IndexOf(key1);
                Message = EmailLog.Body.Substring(index1 + key1.Length);
                int index2 = Message.IndexOf(key2);
                Message = Message.Substring(0, index2);
            }
        }

        
    }
}